import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '*',
    meta: {
      title: '404 Oops...'
    },
    component: () => import('../views/404.vue'),
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'homeView',
    meta: {
      title: '书城',
      keepAlive: true,
    },
    component: Home
  },
  {
    path: '/topRank',
    name: 'topRankView',
    meta: {
      title: '排行榜',
      keepAlive: true,
    },
    component: () => import('../views/TopRank.vue')
  },
  {
    path: '/bookdetail',
    name: 'bookeDetailView',
    meta: {
      title: '书籍详情',
      keepAlive: true,
    },
    component: () => import('../views/BookDetail.vue')
  },
  {
    path: '/search',
    name: 'searchView',
    meta: {
      title: '搜一搜',
      keepAlive: true,
    },
    component: () => import('../views/Search.vue'),
  },
  {
    path: '/chapterlist',
    name: 'chapterView',
    meta: {
      title: '章节目录',
      keepAlive: true,
    },
    component: () => import('../views/ChapterList.vue')
  },
  {
    path: '/article',
    name: 'articleView',
    meta: {
      title: '在线阅读',
    },
    component: () => import('../views/Article.vue')
  },
  {
    path: '/recentread',
    name: 'recentReadView',
    meta: {
      title: '最近阅读',
    },
    component: () => import('../views/RecentRead.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? to.meta.title + ' - 喜阅' : '';
  document.getElementById('app').scrollTo(0, 0);
  next()
})
export default router
